﻿using SorketServer.Common;
using SorketServer.HYModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SorketServer.LedDll;

namespace SorketServer
{
    public class LEDJob
    {
        int hProgram;//节目句柄
        int nResult;
        WATERBORDERINFO info;
        LedDll.FONTPROP fontProp10;
        LedDll.FONTPROP fontProp12;
        LedDll.FONTPROP fontProp14;
        LedDll.PLAYPROP playProp;
        LedDll.COMMUNICATIONINFO CommunicationInfo;
        SystemConfig config = new SystemConfig();
        public LEDJob()
        {
            InitData();
            CommunicationInfo = new LedDll.COMMUNICATIONINFO();//定义一通讯参数结构体变量用于对设定的LED通讯，具体对此结构体元素赋值说明见COMMUNICATIONINFO结构体定义部份注示
            CommunicationInfo.SendType = 0;//设为固定IP通讯模式，即TCP通讯
            CommunicationInfo.IpStr = ConfigurationManager.AppSettings["IP"].ToString(); ;//给IpStr赋值LED控制卡的IP
            CommunicationInfo.LedNumber = 1;//LED屏号为1，注意socket通讯和232通讯不识别屏号，默认赋1就行了，485必需根据屏的实际屏号进行赋值
            hProgram = LedDll.LV_CreateProgram(288, 176, 2);
            config = CarHelper.GetConfig();
        }
        void InitData()
        {
            info = new WATERBORDERINFO()
            {
                BorderColor = LedDll.COLOR_RED,
                BorderSpeed = 4,
                BorderType = 0,
                BorderValue = 0,
                BorderStyle = 0,
                Flag = 0

            };


            fontProp10 = new LedDll.FONTPROP();//文字属性
            fontProp10.FontName = "宋体";
            fontProp10.FontSize = 10;
            fontProp10.FontColor = LedDll.COLOR_RED;
            fontProp10.FontBold = 0;

            fontProp12 = new LedDll.FONTPROP();//文字属性
            fontProp12.FontName = "宋体";
            fontProp12.FontSize = 12;
            fontProp12.FontColor = LedDll.COLOR_RED;
            fontProp12.FontBold = 0;

            fontProp14 = new LedDll.FONTPROP();//文字属性
            fontProp14.FontName = "宋体";
            fontProp14.FontSize = 14;
            fontProp14.FontColor = LedDll.COLOR_RED;
            fontProp14.FontBold = 0;

            playProp = new LedDll.PLAYPROP();
            playProp.InStyle = 25;
            playProp.DelayTime = 3000;
            playProp.Speed = 2;

        }

        LedDll.AREARECT GetReact(int left, int top, int width, int height)
        {
            LedDll.AREARECT AreaRect = new LedDll.AREARECT();//区域坐标属性结构体变量
            AreaRect.left = left;
            AreaRect.top = top;
            AreaRect.width = width;
            AreaRect.height = height;
            return AreaRect;

        }
        void DrawWaterBorder(int proNum, int areaNum, int left, int top, int width, int height, string content, LedDll.FONTPROP fontProp, int nAlign)
        {
            LedDll.AREARECT areaReact = GetReact(left, top, width, height);
            nResult = LedDll.LV_AddWaterBorder(hProgram, proNum, areaNum, ref areaReact, ref info);
            areaReact = GetReact(left + 1, top + 1, width - 2, height - 2);
            LedDll.LV_AddImageTextArea(hProgram, proNum, areaNum + 1, ref areaReact, 0);
            nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, proNum, areaNum + 1, LedDll.ADDTYPE_STRING, content, ref fontProp, ref playProp, nAlign, 2);//通过字符串添加一个多行文本到图文区，参数说明见声明注示

        }

        public void DrawFirst()
        {
            int proNum = 1;
            int areaNum = 1;
            nResult = LedDll.LV_AddProgram(hProgram, proNum, 10, 1);
            DrawWaterBorder(proNum, areaNum, 0, 0, 288, 23, "安全承诺公告", fontProp12, 2);
            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 0, 23, 40, 138, "\r\n\r\n企业状态", fontProp12, 2);

            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 40, 23, 248, 22, "生产装置" + config.ProductNum + "套:其中", fontProp12, 0);
            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 40, 45, 248, 22, "运行" + config.RunNum + "套;停用" + config.StopNum + "套;检修" + config.RepairNum + "套", fontProp12, 0);

            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 40, 67, 248, 47, "特殊动火" + config.SpecialNum + "处; 一级动火" + config.OneNum + "处;\r\n二级动火" + config.TwoNum + "处;", fontProp12, 0);
            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 40, 114, 248, 22, "进入受限空间作业" + config.LimitNum + "处;", fontProp12, 0);
            areaNum += 2;
            string str = Enum.GetName(typeof(EnumYesNo), config.IsTest);
            DrawWaterBorder(proNum, areaNum, 40, 136, 248, 25, "是否处于试生产:" + str, fontProp12, 0);
            areaNum += 2;
            LedDll.AREARECT areaReact = GetReact(0, 161, 288, 15);
            //LedDll.LV_AddImageTextArea(hProgram, proNum, areaNum, ref areaReact, 0);
            // nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, proNum, areaNum, LedDll.ADDTYPE_STRING, config.Anounce, ref fontProp14, ref playProp, 2, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            nResult = LedDll.LV_QuickAddSingleLineTextArea(hProgram, proNum, areaNum, ref areaReact, LedDll.ADDTYPE_STRING, "    "+config.Anounce, ref fontProp14, 1);//快速通过字符添加一个单行文本区域，函数见函数声明注示

        }

        public void DrawSecond()
        {
            int proNum = 2;
            int areaNum = 1;
            nResult = LedDll.LV_AddProgram(hProgram, proNum, 10, 1);
            DrawWaterBorder(proNum, areaNum, 0, 0, 40, 23, "", fontProp14, 2);
            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 0, 23, 40, 138, "\r\n\r\n企业承诺", fontProp14, 2);
            areaNum += 2;
            string str = Enum.GetName(typeof(EnumYesNo), config.IsParking);
            DrawWaterBorder(proNum, areaNum, 40, 0, 248, 23, "是否处于开停车状态:" + str, fontProp12, 0);

            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 40, 23, 248, 92, "\r\n    今天我公司已进行安全风险研判,各项安全风险防控措施已落实到位,我承诺所有生产装置处于安全运行状态。", fontProp12, 0);
            areaNum += 2;
            DrawWaterBorder(proNum, areaNum, 40, 115, 248, 23, "负责人:" + config.ChargeMan, fontProp12, 2);
            areaNum += 2;
            DateTime now = DateTime.Now;
            str = now.Year + "年" + now.Month + "月" + now.Day + "日";
            DrawWaterBorder(proNum, areaNum, 40, 138, 248, 23, str, fontProp12, 2);

            areaNum += 2;
            LedDll.AREARECT areaReact = GetReact(0, 161, 288, 15);
            //LedDll.LV_AddImageTextArea(hProgram, proNum, areaNum, ref areaReact, 0);
            // nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, proNum, areaNum, LedDll.ADDTYPE_STRING, config.Anounce, ref fontProp14, ref playProp, 2, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            nResult = LedDll.LV_QuickAddSingleLineTextArea(hProgram, proNum, areaNum, ref areaReact, LedDll.ADDTYPE_STRING, "    "+config.Anounce, ref fontProp14, 1);//快速通过字符添加一个单行文本区域，函数见函数声明注示

        }
        public void DrawDepart()
        {
            List<DepartCountDto> list = CarHelper.GetDepartCount();
            int page = (int)Math.Ceiling(((decimal)list.Count) / 10);
            for (var i = 1; i <= page; i++)
            {
                nResult = LedDll.LV_AddProgram(hProgram, i + 2, 5, 1);
                CreateCarPlate(i + 2, list.Skip((i - 1) * 8).Take(8).ToList());
            }

        }
        void CreateCarPlate(int no, List<DepartCountDto> dtos)
        {
            int area = 1;
            LedDll.AREARECT areaReact = GetReact(0, 4, 288, 16);
            LedDll.LV_AddImageTextArea(hProgram, no, area, ref areaReact, 0);
            nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, no, area, LedDll.ADDTYPE_STRING, config.Company, ref fontProp14, ref playProp, 2, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            string str1 = "部门\r\n";
            string str2 = "人数\r\n";
            for (var i = 0; i < dtos.Count; i++)
            {
                str1 = str1 + dtos[i].DepartName + "\r\n";
                str2 = str2 + dtos[i].DepartCount + "人" + "\r\n";
            }
            areaReact = GetReact(0, 30, 140, 130);
            area++;
            LedDll.LV_AddImageTextArea(hProgram, no, area, ref areaReact, 0);

            nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, no, area, LedDll.ADDTYPE_STRING, str1, ref fontProp10, ref playProp, 2, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示

            areaReact = GetReact(145, 30, 140, 130);
            area++;
            LedDll.LV_AddImageTextArea(hProgram, no, area, ref areaReact, 0);

            nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, no, area, LedDll.ADDTYPE_STRING, str2, ref fontProp10, ref playProp, 2, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            area += 2;
            areaReact = GetReact(0, 161, 288, 15);
            //LedDll.LV_AddImageTextArea(hProgram, proNum, areaNum, ref areaReact, 0);
            // nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, proNum, areaNum, LedDll.ADDTYPE_STRING, config.Anounce, ref fontProp14, ref playProp, 2, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            nResult = LedDll.LV_QuickAddSingleLineTextArea(hProgram, no, area, ref areaReact, LedDll.ADDTYPE_STRING, "    "+config.Anounce, ref fontProp14, 1);//快速通过字符添加一个单行文本区域，函数见函数声明注示

        }

        public void Save()
        {
            nResult = LedDll.LV_Send(ref CommunicationInfo, hProgram);//发送，见函数声明注示
            LedDll.LV_DeleteProgram(hProgram);
            if (nResult != 0)//如果失败则可以调用LV_GetError获取中文错误信息
            {
                string ErrStr;
                ErrStr = LedDll.LS_GetError(nResult);

            }
            else
            {

            }
        }
    }
    public enum EnumYesNo
    {
        否 = 0,
        是 = 1
    }
}
